package com.gk.ScheduleTask;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.gk.bean.ZXY_GK_CK_BILL_PRO;
import com.gk.bean.ZXY_GK_RK_BILL_PRO;
import com.gk.bean.ZxyUploadbill;
import com.gk.bean.qfdto.GKT340BackJson;
import com.gk.bean.qfdto.GkT320BackJson;
import com.gk.dao.InStockBillDao;
import com.gk.dao.OutStcokBillDao;
import com.gk.dao.ZxyUploadbillDao;
import com.gk.http.entity.HttpResult;
import com.gk.http.service.GetGkDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 获取国控出入库单据定时器
 */
@Component
@Slf4j
public class GetGkBillMultithreadScheduleTask {
    @Resource
    private ZxyUploadbillDao zxyUploadbillDao;
    @Resource
    private GetGkDataService getGkDataService;
    @Resource
    private InStockBillDao inStockBillDao;
    @Resource
    private OutStcokBillDao outStockBillDao;

    @Async
    @Scheduled(fixedDelay = 330000)  //间隔60秒
    //TODO 拉取国控采购订单入库数据
    public void getCgBill() {
        log.info("拉取国控采购订单入库数据定时器任务执行成功");
        List<ZxyUploadbill> list = zxyUploadbillDao.getZxy_UploadbillList(34, 0);
        // 使用set集合存储单据编号
        Set<String> billCodeSets = new HashSet<>();
        for (ZxyUploadbill zxyUploadbill : list) {
            billCodeSets.add(zxyUploadbill.getBillcode());
        }
        for (String billCodeSet : billCodeSets) {
            HttpResult httpResult = getGkDataService.getGkInStockData(billCodeSet);
            if (!httpResult.getCode().equals("0")) {
                log.error(billCodeSet + "拉取数据异常:" + JSON.toJSONString(httpResult));
                continue;
            } else {
                //处理入库数据
                log.info(httpResult.getData());
                GkT320BackJson gkT320BackJson = new GkT320BackJson();
                gkT320BackJson.setData(JSONArray.parseArray(httpResult.getData(), GkT320BackJson.DataBean.class));
                for (GkT320BackJson.DataBean.XmlBean.ContentBean.DetailBean.SubBean.SublineBean subline : gkT320BackJson.getData().get(0).getXml().getContent().getDetail().getSub().getSubline()) {
                    ZXY_GK_RK_BILL_PRO zxy_gk_rk_bill_pro = new ZXY_GK_RK_BILL_PRO();
                    log.info(billCodeSet + "国控返回的采购入库数据" + JSON.toJSONString(subline));
                    zxy_gk_rk_bill_pro.setBillCode(subline.getBillno());
                    zxy_gk_rk_bill_pro.setGkbillCode(subline.getLorderid());
                    zxy_gk_rk_bill_pro.setBillType("34");
                    zxy_gk_rk_bill_pro.setShDate(subline.getArrivedate());//收货时间
                    zxy_gk_rk_bill_pro.setSjDate(subline.getPutupdate());//上架时间
                    zxy_gk_rk_bill_pro.setBrec(subline.getSourcecompanyid());//
                    zxy_gk_rk_bill_pro.setGldetailId(subline.getLorderdtlid());
                    zxy_gk_rk_bill_pro.setBillId(Integer.parseInt(subline.getPorderdtlid()));//
                    zxy_gk_rk_bill_pro.setPrec(subline.getGoodsid());
                    zxy_gk_rk_bill_pro.setJobnumber(subline.getLotno());
                    zxy_gk_rk_bill_pro.setValiddate(subline.getValiddate());
                    zxy_gk_rk_bill_pro.setPrddate(subline.getPrddate());
                    zxy_gk_rk_bill_pro.setQuanstatus(subline.getQuanstatus());
                    zxy_gk_rk_bill_pro.setRealrecvqty(new BigDecimal(subline.getRealrecvqty()));
                    zxy_gk_rk_bill_pro.setCheckdate(subline.getCheckdate().endsWith("") ? subline.getArrivedate() : subline.getCheckdate());
                    zxy_gk_rk_bill_pro.setCheckmana(subline.getCheckmana().equals("") ? "国控验收人A" : subline.getCheckmana());
                    zxy_gk_rk_bill_pro.setCheckmanb(subline.getCheckmanb().equals("") ? "国控验收人B" : subline.getCheckmanb());
                    inStockBillDao.ZXY_GK_RK_BILL_PRO(zxy_gk_rk_bill_pro);
                }

//
            }

        }
        log.info("拉取国控采购订单入库数据定时器任务执行结束");
    }

    @Async
    @Scheduled(fixedDelay = 360000)  //间隔60秒
    //TODO 拉取国控销售退货数据
    public void getXsBackBill() {
        log.info("拉取国控销售退货数据定时器任务执行开始");
        List<ZxyUploadbill> list = zxyUploadbillDao.getZxy_UploadbillList(45, 0);
        // 使用set集合存储单据编号
        Set<String> billCodeSets = new HashSet<>();
        for (ZxyUploadbill zxyUploadbill : list) {
            billCodeSets.add(zxyUploadbill.getBillcode());
        }
        for (String billCodeSet : billCodeSets) {
            HttpResult httpResult = getGkDataService.getGkInStockData(billCodeSet);
            if (!httpResult.getCode().equals("0")) {
                log.error(billCodeSet + "拉取数据异常:" + JSON.toJSONString(httpResult));
                continue;
            } else {
                //处理入库数据
                log.info(httpResult.getData());
                GkT320BackJson gkT320BackJson = new GkT320BackJson();
                gkT320BackJson.setData(JSONArray.parseArray(httpResult.getData(), GkT320BackJson.DataBean.class));
                for (GkT320BackJson.DataBean.XmlBean.ContentBean.DetailBean.SubBean.SublineBean subline : gkT320BackJson.getData().get(0).getXml().getContent().getDetail().getSub().getSubline()) {
                    ZXY_GK_RK_BILL_PRO zxy_gk_rk_bill_pro = new ZXY_GK_RK_BILL_PRO();
                    log.info(billCodeSet + "国控返回的销售退货入库数据" + JSON.toJSONString(subline));
                    zxy_gk_rk_bill_pro.setBillCode(subline.getBillno());
                    zxy_gk_rk_bill_pro.setGkbillCode(subline.getLorderid());
                    zxy_gk_rk_bill_pro.setBillType("45");
                    zxy_gk_rk_bill_pro.setShDate(subline.getArrivedate());//收货时间
                    zxy_gk_rk_bill_pro.setSjDate(subline.getPutupdate());//上架时间
                    zxy_gk_rk_bill_pro.setBrec(subline.getSourcecompanyid());//
                    zxy_gk_rk_bill_pro.setGldetailId(subline.getLorderdtlid());
                    zxy_gk_rk_bill_pro.setBillId(Integer.parseInt(subline.getPorderdtlid()));//
                    zxy_gk_rk_bill_pro.setPrec(subline.getGoodsid());
                    zxy_gk_rk_bill_pro.setJobnumber(subline.getLotno());
                    zxy_gk_rk_bill_pro.setValiddate(subline.getValiddate());
                    zxy_gk_rk_bill_pro.setPrddate(subline.getPrddate());
                    zxy_gk_rk_bill_pro.setQuanstatus(subline.getQuanstatus());
                    zxy_gk_rk_bill_pro.setRealrecvqty(new BigDecimal(subline.getRealrecvqty()));
                    zxy_gk_rk_bill_pro.setCheckdate(subline.getCheckdate().endsWith("") ? subline.getArrivedate() : subline.getCheckdate());
                    zxy_gk_rk_bill_pro.setCheckmana(subline.getCheckmana().equals("") ? "国控验收人A" : subline.getCheckmana());
                    zxy_gk_rk_bill_pro.setCheckmanb(subline.getCheckmanb().equals("") ? "国控验收人B" : subline.getCheckmanb());
                    inStockBillDao.ZXY_GK_RK_BILL_PRO(zxy_gk_rk_bill_pro);
                }
            }

        }

        log.info("拉取国控销售退货数据定时器任务执行结束");
    }

    @Async
    @Scheduled(fixedDelay = 340000)  //间隔60秒
    //TODO 拉取国控销售出库数据
    public void getXsBill() {
        log.info("拉取国控销售出库数据定时器任务执行成功");
        List<ZxyUploadbill> list = zxyUploadbillDao.getZxy_UploadbillList(11, 0);
        // 使用set集合存储单据编号
        Set<String> billCodeSets = new HashSet<>();
        for (ZxyUploadbill zxyUploadbill : list) {
            billCodeSets.add(zxyUploadbill.getBillcode());
        }

        for (String billCodeSet : billCodeSets) {
            HttpResult httpResult = getGkDataService.getGkOutStockData(billCodeSet);
            if (!httpResult.getCode().equals("0")) {
                log.error(billCodeSet + "拉取数据异常:" + JSON.toJSONString(httpResult));
                continue;
            } else {
                //处理入库数据
//                log.info(httpResult.getData());
                GKT340BackJson gkt340BackJson = new GKT340BackJson();
                gkt340BackJson.setData(JSONArray.parseArray(httpResult.getData(), GKT340BackJson.DataBean.class));
                for (GKT340BackJson.DataBean.XmlBean.ContentBean.DetailBean.SubBean.SublineBean subline : gkt340BackJson.getData().get(0).getXml().getContent().getDetail().getSub().getSubline()) {
                    ZXY_GK_CK_BILL_PRO zxy_gk_ck_bill_pro = new ZXY_GK_CK_BILL_PRO();
                    zxy_gk_ck_bill_pro.setGkbillCode(subline.getExporderid());
                    zxy_gk_ck_bill_pro.setBillCode(subline.getBillno());
                    zxy_gk_ck_bill_pro.setBillType("11");
                    zxy_gk_ck_bill_pro.setExpdate(subline.getExpdate());
                    zxy_gk_ck_bill_pro.setGldetailId(subline.getExporderdtlid());
                    zxy_gk_ck_bill_pro.setBillId(Integer.parseInt(subline.getSrcexpdtlno()));
                    zxy_gk_ck_bill_pro.setPrec(subline.getGoodsid());
                    zxy_gk_ck_bill_pro.setJobnumber(subline.getLotno());
                    zxy_gk_ck_bill_pro.setValiddate(subline.getValiddate());
                    zxy_gk_ck_bill_pro.setPrddate(subline.getPrddate());
                    zxy_gk_ck_bill_pro.setQuanstatus(subline.getQuanstatus());
                    zxy_gk_ck_bill_pro.setTradegoodsqty(new BigDecimal(subline.getTradegoodsqty()));
                    zxy_gk_ck_bill_pro.setTrancompany(subline.getTrancompany());
                    zxy_gk_ck_bill_pro.setFhr("国控发货人");
                    zxy_gk_ck_bill_pro.setReview("国控复核人");
                    outStockBillDao.ZXY_GK_CK_BILL_PRO(zxy_gk_ck_bill_pro);

                }

            }
            log.info("拉取国控销售出库数据结果集:" + JSON.toJSONString(httpResult));
        }
        log.info("拉取国控销售出库数据定时器任务执行结束");
    }

    @Async
    @Scheduled(fixedDelay = 350000)  //间隔60秒
    //TODO 拉取国控采购退货数据
    public void getCgBackBill() {
        log.info("拉取国控采购退货数据定时器任务执行成功");
        List<ZxyUploadbill> list = zxyUploadbillDao.getZxy_UploadbillList(6, 0);
        // 使用set集合存储单据编号
        Set<String> billCodeSets = new HashSet<>();
        for (ZxyUploadbill zxyUploadbill : list) {
            billCodeSets.add(zxyUploadbill.getBillcode());
        }
        for (String billCodeSet : billCodeSets) {
            HttpResult httpResult = getGkDataService.getGkOutStockData(billCodeSet);
            if (!httpResult.getCode().equals("0")) {
                log.error(billCodeSet + "拉取数据异常:" + JSON.toJSONString(httpResult));
                continue;
            } else {
                //处理入库数据
                GKT340BackJson gkt340BackJson = new GKT340BackJson();
                gkt340BackJson.setData(JSONArray.parseArray(httpResult.getData(), GKT340BackJson.DataBean.class));
                for (GKT340BackJson.DataBean.XmlBean.ContentBean.DetailBean.SubBean.SublineBean subline : gkt340BackJson.getData().get(0).getXml().getContent().getDetail().getSub().getSubline()) {
                    ZXY_GK_CK_BILL_PRO zxy_gk_ck_bill_pro = new ZXY_GK_CK_BILL_PRO();
                    zxy_gk_ck_bill_pro.setGkbillCode(subline.getExporderid());
                    zxy_gk_ck_bill_pro.setBillCode(subline.getBillno());
                    zxy_gk_ck_bill_pro.setBillType("6");
                    zxy_gk_ck_bill_pro.setExpdate(subline.getExpdate());
                    zxy_gk_ck_bill_pro.setGldetailId(subline.getExporderdtlid());
                    zxy_gk_ck_bill_pro.setBillId(Integer.parseInt(subline.getSrcexpdtlno()));
                    zxy_gk_ck_bill_pro.setPrec(subline.getGoodsid());
                    zxy_gk_ck_bill_pro.setJobnumber(subline.getLotno());
                    zxy_gk_ck_bill_pro.setValiddate(subline.getValiddate());
                    zxy_gk_ck_bill_pro.setPrddate(subline.getPrddate());
                    zxy_gk_ck_bill_pro.setQuanstatus(subline.getQuanstatus());
                    zxy_gk_ck_bill_pro.setTradegoodsqty(new BigDecimal(subline.getTradegoodsqty()));
                    zxy_gk_ck_bill_pro.setTrancompany(subline.getTrancompany());
                    zxy_gk_ck_bill_pro.setFhr("国控发货人");
                    zxy_gk_ck_bill_pro.setReview("国控复核人");
                    outStockBillDao.ZXY_GK_CK_BILL_PRO(zxy_gk_ck_bill_pro);

                }
            }
            log.info("拉取国控采购退货出库数据结果集:" + JSON.toJSONString(httpResult));
        }
        log.info("拉取国控采购退货数据定时器任务执行结束");
    }
//

}
